package cn.guxinhu.billsystem.user.controller;

import cn.guxinhu.billsystem.user.pojo.BillUser;
import cn.guxinhu.billsystem.user.service.impl.UserRegisterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Guxinhu
 * @create 2021-06-21 14:58
 */
@RestController
public class UserRegisterController {
    /**
     * @Description: 注册的控制层
     * @Method: RegisterContrller.register()
     * @Param: /register -- 网页映射路径
     * user(User) -- 前端传入的实体类
     * @Return: [mes(String)]
     * @Author: Guxinhu
     * @Date Created in 2020/08/19 11:37
     */
    @Autowired
    private UserRegisterServiceImpl userRegisterService;

    @RequestMapping("/user/register")
    public String register(BillUser user) {
        String mes = "成功";
        if (user == null)
            return "操作失败";
        try {
            userRegisterService.register(user);
        } catch (Exception e) {
            DuplicateKeyException sqlException = e instanceof DuplicateKeyException ? ((DuplicateKeyException) e) : null;
            if (sqlException != null) {
                mes = "账号已存在";
            } else {
                mes = "操作失败";
            }
        }
        return mes;
    }
}
